package cn.hu.pet.service.impl;

import cn.hu.basic.commonenum.ErrorCode;
import cn.hu.basic.domain.Point;
import cn.hu.basic.exception.CustomException;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.BaiduAuditUtils;
import cn.hu.basic.util.CurrentLoginInfoUtil;
import cn.hu.basic.util.DistanceUtil;
import cn.hu.org.domain.Shop;
import cn.hu.org.mapper.ShopMapper;
import cn.hu.pet.domain.OrderPetAcquisition;
import cn.hu.pet.domain.SearchMasterMsg;
import cn.hu.pet.domain.SearchMasterMsgAuditLog;
import cn.hu.pet.mapper.OrderPetAcquisitionMapper;
import cn.hu.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.hu.pet.mapper.SearchMasterMsgMapper;
import cn.hu.pet.service.ISearchMasterMsgService;
import cn.hu.basic.service.impl.BaseServiceImpl;
import cn.hu.user.domain.Logininfo;
import cn.hu.user.domain.User;
import cn.hu.user.mapper.UserMapper;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hu
 * @since 2022-12-27
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;


    @Override
    @Transactional
    public AjaxResult publish(SearchMasterMsg msg, HttpServletRequest request) throws Exception {
        AjaxResult me = AjaxResult.getInstance();
        //获取当前登录用户的信息
        Logininfo lf = CurrentLoginInfoUtil.currentLoginInfo(request);
        if (lf == null){
            throw new CustomException(ErrorCode.LOGININFO_FAILED.getCode());
        }
        //通过logininfoid获取user信息
        User user = userMapper.findByLoginInfoId(lf.getId());
        msg.setUserId(user.getId());
        //先把msg入库，设置msg信息为待审核
        msg.setState(0);
        searchMasterMsgMapper.add(msg);
        //审核
        Boolean flag = BaiduAuditUtils.TextCensor(msg.getTitle() + msg.getCoatColor() + msg.getName());
        //审核通过，修改状态审核通过，查询50km内的店铺，分配给寻主消息
        if(flag){
            msg.setState(1);
            //保存审核日志信息
            SearchMasterMsgAuditLog smal = new SearchMasterMsgAuditLog();
            smal.setMsgId(msg.getId());
            smal.setState(1);
            smal.setAuditTime(new Date());
            searchMasterMsgAuditLogMapper.add(smal);
            //获取寻主消息地址的经纬度
            Point point = DistanceUtil.getPoint(msg.getAddress());
            //查询出所有的店铺
            List<Shop> shops = shopMapper.findAllAvailableShops();
            //计算50km内最近的店铺
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            if(nearestShop != null){
                msg.setShopId(nearestShop.getId());
            }
        }else{
            //审核不通过，修改状态驳回，发送短信或邮件提醒用户。
            msg.setState(-1);
//            SmsUtil.sendSms(user.getPhone(),"提交的寻主信息不合法，请修改后重新提交");
            System.out.println("提交的寻主信息不合法，请修改后重新提交");
            //保存审核日志信息
            SearchMasterMsgAuditLog smal = new SearchMasterMsgAuditLog();
            smal.setMsgId(msg.getId());
            smal.setState(0);
            smal.setAuditTime(new Date());
            searchMasterMsgAuditLogMapper.add(smal);
            me.setSuccess(false);
        }
        searchMasterMsgMapper.update(msg);
        return me;
    }

    @Override
    public AjaxResult reject(Long id) {
        //查询订单是否在进行中
        List<OrderPetAcquisition> orders = orderPetAcquisitionMapper.findByMsgId(id);
        if(orders.size() > 0){
            for (OrderPetAcquisition order : orders) {
                if(order.getState() == 0){
                    throw new CustomException(ErrorCode.ORDER_PROCESS.getCode());
                }
            }
        }
        //通过寻主消息id查询寻主消息
        SearchMasterMsg msg = searchMasterMsgMapper.findById(id);
        //把寻主消息的shopid置空
        msg.setShopId(null);
        //更新寻主消息
        searchMasterMsgMapper.update(msg);
        return AjaxResult.getInstance();
    }
}
